package cn.com.generaldata.os.service.indicator_evaluation.sustainable_development;

import java.util.HashMap;
import java.util.Map;

import org.apache.commons.lang3.Validate;
import org.nutz.dao.sql.Criteria;

import cn.com.generaldata.jsme_indicator_system.entity.IndicatorEvaluation;
import cn.com.generaldata.os.service.indicator_constant.ProvinceIndicatorConstant;
import cn.com.generaldata.os.service.indicator_evaluation.IndicatorEvaluationCalculateService;
import cn.com.generaldata.os.util.BeanMapper;
import cn.com.generaldata.os.vo.IndicatorAccountVO;

/**
 * 海陆经济协调度评估指标计算类.
 */
public class SeaEconomyCoordinationCalculateService extends IndicatorEvaluationCalculateService {

	private String GOP=ProvinceIndicatorConstant.GOP_INDICATOR_NAME;
	private String GDP=ProvinceIndicatorConstant.GDP_INDICATOR_NAME;
	private String[] citys=ProvinceIndicatorConstant.CITYS;

	@Override
	protected Double calculate(IndicatorEvaluation indicator, IndicatorAccountVO indicatorAccountVO) {
		IndicatorAccountVO iav=BeanMapper.map(indicatorAccountVO, IndicatorAccountVO.class);
		Double sumXY=0d;
		Double sumX=0d;
		Double sumX2=0d;
		Double sumY=0d;
		Double sumY2=0d;
		int i=0;
		for(;i<citys.length;i++)
		{
			iav.setAreaId(citys[i]);
			Double xi =getInputOrAccountingIndicatorValue(iav, iav.getYear(), GOP);
			if(xi==null)
				xi=0d;
			Double gdpValue =getInputOrAccountingIndicatorValue(iav, iav.getYear(), GDP);
			if(gdpValue==null)
				gdpValue=0d;
			Double yi=gdpValue-xi;
			sumXY+=xi*yi;
			sumX+=xi;
			sumY+=yi;
			sumX2+=Math.pow(xi, 2);
			sumY2+=Math.pow(yi, 2);
		}
		return ((i*sumXY)-(sumX*sumY))/(Math.sqrt((i*sumX2)-(Math.pow(sumX, 2)))*Math.sqrt((i*sumY2)-(Math.pow(sumY, 2))));
	}
	
}
